package org.cogaen.sound;

import java.io.DataInputStream;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;

/**
 * @class SoundEffect
 * @author Florian Lettner
 *
 * Manages audio data that represents an audio effect.
 */
public class SoundEffect {

	/** The sampledata used for playing the file. */
	private byte[] sampledata;
	
	/** The playback format for the effect. */
	private AudioFormat playbackFormat;
	
	/**
	 * Creates a new sound effect from a certain location.
	 * 
	 * @param audioStream The source of the audio data.
	 */
	public SoundEffect(AudioInputStream audioStream) {

		if (audioStream == null)
			return;
		
		playbackFormat = audioStream.getFormat();
		
		int length = (int) (audioStream.getFrameLength() * audioStream.getFormat().getFrameSize());
		
		sampledata = new byte[length];
		DataInputStream is = new DataInputStream(audioStream);
		
		try {
			is.readFully(sampledata);
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
	
	/**
	 * Provides the caller with the audio stream's playback format.
	 * 
	 * @return The effect's playback format.
	 */
	public AudioFormat getPlaybackFormat() {
		return playbackFormat;
	}
	
	/**
	 * Provides the caller with the sound stream.
	 * 
	 * @return The effect data as byte array.
	 */
	public byte[] getStream() {
		return sampledata;
	}
}
